﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ChanTai.WebApi.Data;
using Newtonsoft.Json;

namespace TestClient
{
    public partial class Form1 : Form
    {
        private HttpClient client;
        public Form1()
        {
            client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:8819/ChanTai/");
            // 为JSON格式添加一个Accept报头
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text)) return;
                var req = new AccSaveRequest() { test = true,args = JsonConvert.DeserializeObject<GroupSubmit>(textBox2.Text),autor = ""};
                var response = client.PostAsJsonAsync(textBox1.Text, req).Result;
                response.EnsureSuccessStatusCode();
                var result = response.Content.ReadAsAsync<Result>().Result;
                if (!result.succeed)
                {
                    textBox3.Text = result.message;
                }
                else
                {
                    textBox3.Text = response.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception err)
            {
                textBox2.Text = err.ToString();
            }
            
        }
    }
}
